package com.dingup.api.Util;

import com.dingup.api.enums.log.ClassInfoLogEnum;
import com.dingup.api.enums.log.LessonInfoLogEnum;
import com.dingup.api.enums.log.PkLessonLogEnum;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.api.model.lesson.LessonInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.enums.Enum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wancheng on 2018/8/20.
 */
public class LoggerUtil {

    public static Logger getLogger(Class<?> clazz) {
        Logger logger = LoggerFactory.getLogger(clazz);
        return logger;
    }

    public static List<String> getUpdateAttribute(Object older, Object update, Class<?> clazz) throws Exception {
        List<String> updateList = new ArrayList<>();
        Class olderClazz = older.getClass();
        Class updateClaszz = update.getClass();
        if (!clazz.equals(olderClazz) && clazz.equals(updateClaszz)) {
            throw new Exception("class is not equal");
        }

        Field[] fields = clazz.getDeclaredFields();
        Arrays.stream(fields).forEach(
                f -> {
                    String fieldName = f.getName();
                    try {
                        Field olderField = olderClazz.getDeclaredField(fieldName);
                        olderField.setAccessible(true);
                        Object olderObject = olderField.get(older);
                        Field updateField = updateClaszz.getDeclaredField(fieldName);
                        updateField.setAccessible(true);
                        Object updateObject = updateField.get(update);
                        if (updateObject != null && !updateObject.equals(olderObject)) {
                            String desc = getDescription(clazz,fieldName);
                            if (!StringUtils.isEmpty(desc)){
                                updateList.add(desc);
                            }
                        }
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
        );

        return updateList;
    }
    public static List<String> getUpdateFileName(Object older, Object update, Class<?> clazz) throws Exception {
        List<String> updateList = new ArrayList<>();
        Class olderClazz = older.getClass();
        Class updateClaszz = update.getClass();
        if (!clazz.equals(olderClazz) && clazz.equals(updateClaszz)) {
            throw new Exception("class is not equal");
        }
        Field[] fields = clazz.getDeclaredFields();
        Arrays.stream(fields).forEach(
                f -> {
                    String fieldName = f.getName();
                    try {
                        Field olderField = olderClazz.getDeclaredField(fieldName);
                        olderField.setAccessible(true);
                        Object olderObject = olderField.get(older);
                        Field updateField = updateClaszz.getDeclaredField(fieldName);
                        updateField.setAccessible(true);
                        Object updateObject = updateField.get(update);
                        if (updateObject != null && !updateObject.equals(olderObject)) {
                            updateList.add(fieldName);
                        }
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
        );

        return updateList;
    }
    public static String getDescription(Class<?> clazz,String attr){
        if (clazz.equals(ClassInfo.class)){
            return ClassInfoLogEnum.getDescription(attr);
        }else if (clazz.equals(LessonInfo.class)){
            return LessonInfoLogEnum.getDescription(attr);
        }
        return null;
    }

    public static void main(String[] arg) {

        ClassInfo older = new ClassInfo();
        older.setId(1);
        older.setClassName("1111");

        ClassInfo update = new ClassInfo();
        update.setId(1);
        update.setClassName("2222");
        update.setClassType("sds");
        List<String> updateList = null;
        try {
            updateList = getUpdateAttribute(older, update, ClassInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.print("");
    }
}
